import {cateAction2} from "../funnames";
import fly from "../../net/flyio";
import url from "../../net/serverurl";
import Arrayutils from "../../utils/arrayutils";
import cclong from "../../utils/globalfun";
import tools from "../tools";
import {getGoodsList, getGoodsCount} from "../../net/commonapi";

const state = {
  selectFemaleCateList: [],
  selectBrandList: [],
  priceData: {
    min_discount: 1.0,
    max_discount: 10.0,
    leftValue: 1,
    rightValue: 10,
  },
  selectManCateList: [],
  otherPrams: {},
  brandList: {},
  categoryData: {},
  currentCateItem: {},
  word: '',
  sortId: 1,
  promotionId: '',
  sortType: 1,
  totalCount: 0
};

const mutations = {
  [cateAction2.ACTION_GOODS_COUNT_UPDATE](state, total) {
    state.totalCount = total;
  },
  [cateAction2.ACTION_SET_CATE](state, cate) {
    state.categoryData = cate;
  },
  [cateAction2.ACTION_SET_CATE_ITEM](state, cate) {
    state.currentCateItem = cate;
  },
  [cateAction2.ACTION_CLEAR_ALL](state) {
    tools.clearAll({
      selectBrandList: state.selectBrandList,
      selectFemaleCateList: state.selectFemaleCateList,
      selectManCateList: state.selectManCateList,
      otherPrams: state.otherPrams,
      price: state.priceData
    })
  },
  [cateAction2.ACTION_RESET](state) {
    state.selectBrandList = []
    state.brandList = {}
    state.selectFemaleCateList = []
    state.selectManCateList = []
    state.otherPrams = {}
    state.sortId = 1
    state.sortType = 1
    state.totalCount = 0
    state.categoryData = {}
    state.currentCateItem = {}
    state.priceData = {
      min_discount: 1.0,
      max_discount: 10.0,
      leftValue: 1,
      rightValue: 10,
    }
  },
  [cateAction2.ACTION_CONDITION_UPDATE](state, brandData) {
    state.brandList = brandData.children;
  },
  [cateAction2.ACTION_PRICE_UPDATE](state, {lowPrice, heighPrice, leftValue, rightValue}) {
    if (!cclong.isNull(lowPrice)) {
      state.priceData.min_price = lowPrice
    }
    if (!cclong.isNull(heighPrice)) {
      state.priceData.max_price = heighPrice
    }
    if (!cclong.isNull(leftValue)) {
      state.priceData.min_discount = Math.min(leftValue, rightValue).toFixed(1)
      state.priceData.leftValue = leftValue
    }
    if (!cclong.isNull(rightValue)) {
      state.priceData.max_discount = Math.max(leftValue, rightValue).toFixed(1)
      state.priceData.rightValue = rightValue
    }
  },
  [cateAction2.ACTION_SELECT_BRAND](state, item) {
    item.checked = !item.checked
    if (item.checked) {
      state.selectBrandList = state.selectBrandList.concat(item);
    } else {
      Arrayutils.removeByIndex(state.selectBrandList, Arrayutils.findIndex(state.selectBrandList, item));
    }
  }
  ,
  [cateAction2.ACTION_SORT_UPDATE]
    (state, {sortId, sortType}) {
    state.sortId = sortId
    state.sortType = sortType
  }
  ,
  [cateAction2.ACTION_WORD_UPDATE](state, {word}) {
    state.word = word
  },
  [cateAction2.ACTION_SET_PROMOTION_ID](state, {promotionId}) {
    state.promotionId = promotionId
  }
  ,
  [cateAction2.ACTION_UPDATE_CATE](state, {key, data, type}) {
    if (type == 1) {
      state.selectFemaleCateList = data;
    } else if (type == 2) {
      state.selectManCateList = data;
    } else {
      if (!state.otherPrams[key]) {
        state.otherPrams[key] = new Array();
      }
      state.otherPrams[key] = data;
    }
  }
};

const getters = {
  [cateAction2.ACTION_GET_CATE_CHECKED](state) {
    if (cclong.isNull(state.otherPrams)) {
      return null
    }
    return state.otherPrams[state.currentCateItem.key]
  }
}

const actions = {
  async getGoodsCount({state, commit}) {
    let paramsObj = tools.getAllCondition({
      selectBrandList: state.selectBrandList,
      selectFemaleCateList: state.selectFemaleCateList,
      selectManCateList: state.selectManCateList,
      otherPrams: state.otherPrams,
      price: state.priceData,
      word: state.word,
      sort_type: state.sortType,
      sort_id: state.sortId,
      promotion_id: state.promotionId
    })
    let resData = await getGoodsCount(paramsObj)
    if (resData.successCode) {
      commit(cateAction2.ACTION_GOODS_COUNT_UPDATE, resData.data.total)
    } else {
      cclong.showLongToast(resData.message)
    }
  }
};

export default {
  state,
  mutations,
  actions,
  getters
};
